/*
 * LightDisplayGUI.java
 *
 * Created on March 28, 2008, 10:57 AM
 */

package org.sunspotworld;

import com.sun.spot.io.j2me.radiogram.Radiogram;
import com.sun.spot.io.j2me.radiogram.RadiogramConnection;
import java.awt.Color;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author  me
 */
public class LightDisplayGUI extends javax.swing.JFrame {
    private JPanel [][] barGraph = new JPanel[20][5];
    private static final String PORT = "45";
    private static LightDisplayGUI gui = new LightDisplayGUI();
    
    public static LightDisplayGUI getInstance() {
        return gui;
    }
    
    public void listenRadio() {
        boolean avoiding;
        int light;
        int distance;
        RadiogramConnection rcvConn = null;
        Radiogram rdg;
        int dst;
        int lgt;
        while(true) {
            try {
                rcvConn = (RadiogramConnection)Connector.open("radiogram://:" + PORT);
                rdg = (Radiogram)rcvConn.newDatagram(rcvConn.getMaximumLength());
                while (true) {
                    rdg.reset();
                    rcvConn.receive(rdg);
                    avoiding = rdg.readBoolean();
                    light = rdg.readInt();
                    distance = rdg.readInt();
                    dst = 0;
                    lgt = 0;
                    obstacleLabel.setText(""+avoiding);
                    lightLabel.setText(""+light);
                    ultrasoundLabel.setText(""+distance);
                    for (int i=0; i < 16; i++) {
                        dst += 6;
                        barGraph[17-i][1].setVisible(light > lgt);
                        barGraph[17-i][3].setVisible(distance > dst);
                        lgt += 50;
                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                if (rcvConn != null) {
                    try {
                        rcvConn.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
    }
    
    private Thread listenThread = new Thread() {
        public void run() {
            LightDisplayGUI.getInstance().listenRadio();
        }
    };
    
    /** Creates new form LightDisplayGUI */
    public LightDisplayGUI() {
        initComponents();
        JLabel lightGraphLabel = new JLabel("Light");
        JLabel distGraphLabel  = new JLabel("Distance");
        lightGraphLabel.setFont(new java.awt.Font("Lucida Grande", 0, 36));
        distGraphLabel.setFont(new java.awt.Font("Lucida Grande", 0, 36));
        for (int j=0; j<20; j++) {
            for (int i = 0; i < 5; i++) {
                barGraph[j][i] = new JPanel();
                if (j > 1 && j < 18) {
                    if (i == 1) {
                        barGraph[j][i].setBackground(Color.RED);
                        barGraph[j][i].setVisible(false);
                        barGraph[j][i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
                    }
                    if (i == 3) {
                        barGraph[j][i].setBackground(Color.BLUE);
                        barGraph[j][i].setVisible(false);
                        barGraph[j][i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
                    }
                }else if (j == 18) {
                    if (i == 1) {
                        barGraph[j][i].add(lightGraphLabel);
                    }
                    if (i==3) {
                        barGraph[j][i].add(distGraphLabel);
                    }
                }
                jPanel2.add(barGraph[j][i]);
            }
        }
        listenThread.start();
        //setVisible(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        lightLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        ultrasoundLabel = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        obstacleLabel = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 48));
        jLabel1.setText("Light Sensor:");

        lightLabel.setFont(new java.awt.Font("Lucida Grande", 0, 48));
        lightLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lightLabel.setText("0");

        jLabel3.setFont(new java.awt.Font("Lucida Grande", 0, 48));
        jLabel3.setText("Ultrasound:");

        ultrasoundLabel.setFont(new java.awt.Font("Lucida Grande", 0, 48));
        ultrasoundLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        ultrasoundLabel.setText("0");

        jLabel4.setFont(new java.awt.Font("Lucida Grande", 0, 48));
        jLabel4.setText("Obstacle:");

        obstacleLabel.setFont(new java.awt.Font("Lucida Grande", 0, 48));
        obstacleLabel.setText("false");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(jPanel1Layout.createSequentialGroup()
                            .add(jLabel1)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(lightLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 90, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(jPanel1Layout.createSequentialGroup()
                            .add(jLabel3)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(ultrasoundLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 90, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel4)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(obstacleLabel)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(66, 66, 66)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lightLabel)
                    .add(jLabel1))
                .add(52, 52, 52)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(ultrasoundLabel))
                .add(58, 58, 58)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(obstacleLabel))
                .addContainerGap(529, Short.MAX_VALUE))
        );

        jPanel2.setLayout(new java.awt.GridLayout(20, 5));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 879, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                LightDisplayGUI.getInstance().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lightLabel;
    private javax.swing.JLabel obstacleLabel;
    private javax.swing.JLabel ultrasoundLabel;
    // End of variables declaration//GEN-END:variables
    
}
